/*
 * CreateResourceDialog.java
 *
 * Created on 19 oktober 2009, 23:01
 */

package mop.gui.resource;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import mop.controller.ResourceController;
import mop.exception.IllegalReservationException;
import mop.exception.IllegalResourceException;
import mop.exception.UnavailableResourceException;
import mop.gui.main.MainWindow;

/**
 *
 * @author  Laurens
 */
public class MakeReservationDialog extends javax.swing.JDialog {
    
    /** Resource description. */
    private String resource;

    /** Creates new form MakeReservationDialog */
    public MakeReservationDialog(java.awt.Frame parent, boolean modal, String resource) {
        super(parent, modal);
        initComponents();
        this.resource = resource;
        this.txtDescription.setText(resource);
        loadReservations();
    }
    
    /**
     * Loads the reservations list.
     */
    private void loadReservations()
    {
        try
        {
            LinkedList<String> reservations = (LinkedList<String>) ResourceController.getInstance().getReservations(resource);
        
            DefaultListModel model = new DefaultListModel();
            for(String r : reservations)
                model.addElement(r);

            this.listReservations.setModel(model);
        }
        catch (IllegalResourceException ex)
        {
            JOptionPane.showMessageDialog(null, "Illegal resource!", "Error", JOptionPane.ERROR_MESSAGE);
            this.dispose();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblMakeReservation = new javax.swing.JLabel();
        lblDescription = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescription = new javax.swing.JTextArea();
        btnMakeReservation = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        lblReservations = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        listReservations = new javax.swing.JList();
        lblStart = new javax.swing.JLabel();
        txtStart = new javax.swing.JTextField();
        txtWeeks = new javax.swing.JTextField();
        lblWeeks = new javax.swing.JLabel();
        lblDays = new javax.swing.JLabel();
        txtDays = new javax.swing.JTextField();
        lblHours = new javax.swing.JLabel();
        lblMinutes = new javax.swing.JLabel();
        txtHours = new javax.swing.JTextField();
        txtMinutes = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Make Reservation");

        lblMakeReservation.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblMakeReservation.setText("Make Reservation");
        lblMakeReservation.setName("lblMakeReservation"); // NOI18N

        lblDescription.setText("Resource");
        lblDescription.setName("lblDescription"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        txtDescription.setColumns(20);
        txtDescription.setEditable(false);
        txtDescription.setRows(5);
        txtDescription.setName("txtDescription"); // NOI18N
        jScrollPane1.setViewportView(txtDescription);

        btnMakeReservation.setText("Make Reservation");
        btnMakeReservation.setName("btnMakeReservation"); // NOI18N
        btnMakeReservation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMakeReservationActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.setName("btnCancel"); // NOI18N
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        lblReservations.setText("Reservations");
        lblReservations.setName("lblReservations"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        listReservations.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        listReservations.setEnabled(false);
        listReservations.setName("listReservations"); // NOI18N
        jScrollPane2.setViewportView(listReservations);

        lblStart.setText("Start date and time");
        lblStart.setName("lblStart"); // NOI18N

        txtStart.setText("DD-MM-YYYY HH:MM");
        txtStart.setName("txtStart"); // NOI18N

        txtWeeks.setText("0");
        txtWeeks.setName("txtWeeks"); // NOI18N

        lblWeeks.setText("Duration - Weeks");
        lblWeeks.setName("lblWeeks"); // NOI18N

        lblDays.setText("Days");
        lblDays.setName("lblDays"); // NOI18N

        txtDays.setText("0");
        txtDays.setName("txtDays"); // NOI18N

        lblHours.setText("Hours");
        lblHours.setName("lblHours"); // NOI18N

        lblMinutes.setText("Minutes");
        lblMinutes.setName("lblMinutes"); // NOI18N

        txtHours.setText("0");
        txtHours.setName("txtHours"); // NOI18N

        txtMinutes.setText("0");
        txtMinutes.setName("txtMinutes"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtStart, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(lblMakeReservation)
                    .addComponent(lblDescription)
                    .addComponent(lblReservations)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtHours, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtWeeks, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnMakeReservation, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                            .addComponent(lblStart, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblWeeks, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblHours, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblMinutes)
                            .addComponent(lblDays)
                            .addComponent(btnCancel, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                            .addComponent(txtDays, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                            .addComponent(txtMinutes, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblMakeReservation)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblDescription)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblReservations)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblStart)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblWeeks)
                    .addComponent(lblDays, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtWeeks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDays, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblHours)
                    .addComponent(lblMinutes))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtHours, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtMinutes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnMakeReservation))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

/**
 * Makes a reservation.
 * 
 * @param   evt
 */
private void btnMakeReservationActionPerformed(java.awt.event.ActionEvent evt)
{                                                   
    // Parse start time/date.
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm");
    Timestamp start = null;
    try {
        start = new Timestamp(((Date) formatter.parse(this.txtStart.getText())).getTime());
    } catch (ParseException ex) {
        JOptionPane.showMessageDialog(null, "Wrong start date and time!", "Error", JOptionPane.ERROR_MESSAGE);
        return;
    }
    
    // Load duration.
    long ms = 0;
    ms += Long.parseLong(this.txtMinutes.getText()) * 60000; // 1 minute = 60k ms
    ms += Long.parseLong(this.txtHours.getText()) * 3600000; // 1 hour = 3.6m ms
    ms += Long.parseLong(this.txtDays.getText()) * 86400000; // 1 day = 86.4m ms
    ms += Long.parseLong(this.txtWeeks.getText()) * 604800000; // 1 week = 604.8m ms
    Timestamp duration = new Timestamp(ms);
    
    // Calculate end.
    Timestamp end = new Timestamp(start.getTime() + duration.getTime());
    
    // Add the reservation.
    try {
        ResourceController.getInstance().addReservation(resource, start, end);
        JOptionPane.showMessageDialog(null, "Reservation made!", "Success", JOptionPane.INFORMATION_MESSAGE);
        MainWindow.getInstance().update();
        this.dispose();
    } catch (IllegalResourceException ex) {
        JOptionPane.showMessageDialog(null, "Illegal resource!", "Error", JOptionPane.ERROR_MESSAGE);
    } catch (IllegalReservationException ex) {
        JOptionPane.showMessageDialog(null, "Wrong reservation date and time (or duration)!", "Error", JOptionPane.ERROR_MESSAGE);
    } catch (UnavailableResourceException ex) {
        JOptionPane.showMessageDialog(null, "Resource is not available at that time!", "Error", JOptionPane.ERROR_MESSAGE);
    }
}                                                                                                   

/**
 * Closes the dialog.
 * 
 * @param   evt
 */
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
    this.dispose();
}//GEN-LAST:event_btnCancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnMakeReservation;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblDays;
    private javax.swing.JLabel lblDescription;
    private javax.swing.JLabel lblHours;
    private javax.swing.JLabel lblMakeReservation;
    private javax.swing.JLabel lblMinutes;
    private javax.swing.JLabel lblReservations;
    private javax.swing.JLabel lblStart;
    private javax.swing.JLabel lblWeeks;
    private javax.swing.JList listReservations;
    private javax.swing.JTextField txtDays;
    private javax.swing.JTextArea txtDescription;
    private javax.swing.JTextField txtHours;
    private javax.swing.JTextField txtMinutes;
    private javax.swing.JTextField txtStart;
    private javax.swing.JTextField txtWeeks;
    // End of variables declaration//GEN-END:variables

}
